import { Toast } from "vant";
import cStoreList from "components/storeList";
import DList from "@/components/dlist";

export default {
  data() {
    return {
      show: false,
      store_title: "",
      communityStoreDetail: {},
      communityStoreId: 0,
      point: {},
      address: "",
      city: "",
      search: "",

      storeList: [],
      loading: false,
      page: 1,
      isLoadMore: true,
      openTemplate: 0
    };
  },
  created() {
    if (this.$route.params.fromHome !== 1) {
      this.initData();
      this.ready();
    }
  },
  activated() {
    this.show = false;
    this.search = "";
    this.communityStoreId = this.$route.query.id;
    if (!this.communityStoreId) {
      this.$router.push(this.fun.getUrl("communityStore"));
    }
    if (this.$route.params.fromHome === 1 || this.$store.state.referer) {
      this.$store.commit("setReferer", "");
      this.communityStoreDetail = {};
      this.initData();
      this.ready();
    }
  },
  methods: {
    initData() {
      this.loading = false;
      this.page = 1;
      this.isLoadMore = true;
      this.storeList = [];
    },
    tolocation() {
      this.$store.commit("setReferer", window.location.href);
      this.$router.push(this.fun.getUrl("o2oLocation", {}, { tag: "community" }));
    },
    searchStore() {
      this.initData();
      this.getDetails();
    },
    getDetails() {
      if (this.loading === true || this.isLoadMore === false) {
        return;
      }
      this.loading = true;
      $http
        .get(
          "plugin.store-community.frontend.community.getInfo",
          {
            lng: this.point.lng,
            lat: this.point.lat,
            community_id: this.communityStoreId,
            page: this.page,
            kwd: this.search
          },
          ""
        )
        .then(res => {
          if (res.result === 1) {
            this.store_title = res.data.plugin_name || "社区门店";
            this.fun.setWXTitle(this.store_title);
            this.communityStoreDetail = res.data.info || {};
            this.page++;
            if (res.data.store_list.current_page === res.data.store_list.last_page || res.data.store_list.data.length === 0 || res.data.store_list.data.length < res.data.store_list.per_page) {
              this.isLoadMore = false;
            }
            if (res.data.store_list) {
              this.storeList = this.storeList.concat(res.data.store_list.data);
            }
            this.loading = false;
          } else {
            console.log(res.msg);
          }
        })
        .catch(error => {
          console.error(error);
        });
    },
    enterSearch(event) {
      if (event.keyCode === 13) {
        //如果按的是enter键
        event.preventDefault();
        this.searchStore();
      }
    },
    ready() {
      // let myLocation = this.$store.state.o2oLocation;
      // console.info(myLocation);
      // if (myLocation.point) {
      //   this.address = myLocation.title;
      //   this.point = myLocation.point;
      //   this.city = myLocation.city;
      //   this.getDetails();
      // } else {
      // }

      this.fun
        .getLocation()
        .then(res => {
          this.address = res.title;
          this.point = res.point;
          this.city = res.city;
          this.getDetails();
        })
        .catch(err => {
          console.log(err);
        });
    }
  },
  components: {
    cStoreList,
    DList
  }
};
